home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / cap / ogl_interfere.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  11KB  |  419 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <X11/Intrinsic.h>
  19. #include <GL/gl.h>
  20. #include <GL/glu.h>
  21. #include "interfere.h"
  22. #include "ogl_shapes.h"
  23.  
  24. #define STEN_MASK ((1 << sten_size) - 1)
  25.  
  26. #define PLASTIC_SPECULAR 0.8
  27. #define PLASTIC_SHININESS 10.
  28. #define PLANE_LENGTH 2.
  29. #define NUM_CLIP_PLANES 1
  30. #define MAX_CLIP_PLANE 6
  31. #define ANGLE_DELTA 1
  32. #define VIEWPOINT_Z 8.
  33. #define NUMBER(array) ((unsigned long) (sizeof(array) / sizeof(array[0])))
  34.  
  35. enum
  36. {
  37.     VIEWING_TRANSFORMATION = 0,
  38.     CLIPPING_TRANSFORMATION,
  39.     MODEL_TRANSFORMATION = CLIPPING_TRANSFORMATION + MAX_CLIP_PLANE
  40. };
  41.  
  42. typedef GLvoid (*Method) (GLvoid);
  43.  
  44. typedef struct tagMaterial
  45. {
  46.     GLfloat color[4];
  47.     GLfloat specular[4];
  48.     GLfloat shininess;
  49. } Material, * MaterialPtr;
  50.  
  51. typedef struct tagTransformation
  52. {
  53.     GLfloat depth;
  54.     GLfloat uniform_scale;
  55.     GLuint x_rot, y_rot, z_rot;
  56. } Transformation, * TransformationPtr;
  57.  
  58. typedef struct tagShape
  59. {
  60.     Method constructor;
  61.     GLuint object, material_object;
  62.     Material material;
  63.     Transformation transform;
  64. } Shape, * ShapePtr;
  65.  
  66. static Shape shapes[] = {
  67.     {doSolidTorus, 0, 0,
  68.      {{0.1, 0.8, 0.1, 1.}, 
  69.       {PLASTIC_SPECULAR, PLASTIC_SPECULAR, PLASTIC_SPECULAR, 1.}, 
  70.       PLASTIC_SHININESS}, 
  71.      {0., 1., 45., 0., 0.}},
  72.     {doSolidTorus, 0, 0,
  73.      {{0.1, 0.1, 0.8, 1.}, 
  74.       {PLASTIC_SPECULAR, PLASTIC_SPECULAR, PLASTIC_SPECULAR, 1.}, 
  75.       PLASTIC_SHININESS}, 
  76.      {0., 1., 90., 0., 0.}},
  77. };
  78.  
  79. static Transformation transformations[] = 
  80. {
  81.     {VIEWPOINT_Z, 1., 20, 340, 0},
  82.     {0., 1., 0, 0, 0},
  83.     {0., 1., 0, 90, 0},
  84.     {0., 1., 0, 0, 90},
  85.     {0., 1., 0, 0, 0},
  86.     {0., 1., 0, 0, 0},
  87.     {0., 1., 0, 0, 0},
  88.     {0., 1., 0, 0, 0}
  89. };
  90.  
  91. static GLvoid
  92. setupProjection (GLvoid)
  93. {
  94.     glMatrixMode(GL_PROJECTION);
  95.     glLoadIdentity();
  96.     gluPerspective(60., 1., 0.25, 10000.);
  97.     glMatrixMode(GL_MODELVIEW);
  98. }
  99.  
  100. static GLvoid
  101. defineLighting (GLvoid)
  102. {
  103.     GLfloat light_model_ambient[] = {0.2, 0.2, 0.2, 1.};
  104.     GLfloat light0_position[] = {1., 1., 1, 0.};
  105.  
  106.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  107.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
  108.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
  109.  
  110.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  111.  
  112.     glEnable(GL_LIGHT0);
  113.     glShadeModel(GL_SMOOTH);
  114. }
  115.  
  116. static GLvoid
  117. whiteAxes (GLvoid)
  118. {
  119.     GLfloat origin[] = {0., 0., 0.};
  120.     GLfloat x_unit[] = {1., 0., 0.};
  121.     GLfloat y_unit[] = {0., 1., 0.};
  122.     GLfloat z_unit[] = {0., 0., 1.};
  123.     GLfloat axes_color[] = {1., 1., 1.};
  124.  
  125.      glPushMatrix();
  126.       glColor3fv(axes_color);
  127.       glScalef(2., 2., 2.);
  128.       glBegin(GL_LINES);
  129.        glVertex3fv(origin);
  130.        glVertex3fv(x_unit);
  131.       glEnd();
  132.       glBegin(GL_LINES);
  133.        glVertex3fv(origin);
  134.        glVertex3fv(y_unit);
  135.       glEnd();
  136.       glBegin(GL_LINES);
  137.        glVertex3fv(origin);
  138.        glVertex3fv(z_unit);
  139.       glEnd();
  140.      glPopMatrix();
  141. }
  142.  
  143. static GLvoid
  144. initShapes (GLvoid)
  145. {
  146.     GLuint i = 0;
  147.     GLuint base = glGenLists(NUMBER(shapes));
  148.     GLuint mat_base = glGenLists(NUMBER(shapes));
  149.     GLfloat mv_matrix[16];
  150.  
  151.     initStockShapes();
  152.     for (i = 0; i < NUMBER(shapes); i++) {
  153.         shapes[i].object = base + i;
  154.         shapes[i].material_object = mat_base + i;
  155.         glNewList(shapes[i].material_object, GL_COMPILE);
  156.          glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, 
  157.                       shapes[i].material.color);
  158.          glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, 
  159.                       shapes[i].material.specular);
  160.          glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 
  161.                      shapes[i].material.shininess);
  162.         glEndList();
  163.  
  164.         glPushMatrix();
  165.          glScalef(shapes[i].transform.uniform_scale,
  166.                     shapes[i].transform.uniform_scale,
  167.                    shapes[i].transform.uniform_scale);
  168.          glTranslatef(0., 0., shapes[i].transform.depth);
  169.          glRotatef(shapes[i].transform.x_rot, 1., 0., 0.);
  170.          glRotatef(shapes[i].transform.y_rot, 0., 1., 0.);
  171.          glRotatef(shapes[i].transform.z_rot, 0., 0., 1.);
  172.          glGetFloatv(GL_MODELVIEW_MATRIX, mv_matrix);
  173.         glPopMatrix();
  174.  
  175.         glPushMatrix();
  176.          glNewList(shapes[i].object, GL_COMPILE);
  177.            glMultMatrixf(mv_matrix);
  178.            glCallList(shapes[i].material_object);
  179.            (*(shapes[i].constructor))();
  180.          glEndList();
  181.         glPopMatrix();
  182.     }
  183. }
  184.  
  185. void 
  186. initGraphics (void)
  187. {
  188.     glEnable(GL_DEPTH_TEST);
  189.     glEnable(GL_AUTO_NORMAL);
  190.     glCullFace(GL_BACK);
  191.     glClear(GL_STENCIL_BUFFER_BIT);
  192.     glDepthRange(0, 1);
  193.     setupProjection();
  194.     defineLighting();
  195.     initShapes();
  196. }
  197.  
  198. static GLvoid
  199. doViewingTransformation (void)
  200. {
  201.     glTranslatef(0., 0., -transformations[VIEWING_TRANSFORMATION].depth);
  202.     glRotatef(transformations[VIEWING_TRANSFORMATION].x_rot, 1., 0., 0.);
  203.     glRotatef(transformations[VIEWING_TRANSFORMATION].y_rot, 0., 1., 0.);
  204.     glRotatef(transformations[VIEWING_TRANSFORMATION].z_rot, 0., 0., 1.);
  205. }
  206.  
  207. static GLvoid
  208. doTransformation (unsigned int index)
  209. {
  210.     doViewingTransformation();
  211.     glScalef(transformations[index].uniform_scale, 
  212.              transformations[index].uniform_scale,
  213.              transformations[index].uniform_scale);
  214.     glTranslatef(0., 0., transformations[index].depth);
  215.     glRotatef(transformations[index].x_rot, 1., 0., 0.);
  216.     glRotatef(transformations[index].y_rot, 0., 1., 0.);
  217.     glRotatef(transformations[index].z_rot, 0., 0., 1.);
  218. }
  219.  
  220. static GLvoid
  221. doClippingTransformation (GLuint clipping_plane)
  222. {
  223.     doTransformation(CLIPPING_TRANSFORMATION + clipping_plane);
  224. }
  225.  
  226. static GLvoid
  227. drawClipPlanes (GLvoid)
  228. {
  229.     GLfloat clipplane_vertices[4][3] = {
  230.         {PLANE_LENGTH, -PLANE_LENGTH, 0.},
  231.         {-PLANE_LENGTH, -PLANE_LENGTH, 0.},
  232.         {-PLANE_LENGTH, PLANE_LENGTH, 0.},
  233.         {PLANE_LENGTH, PLANE_LENGTH, 0.},
  234.     };
  235.     register unsigned int i = 0;
  236.  
  237.     glColor3ub(0xff, 0xff, 0x0);
  238.  
  239.     for (i = 0 ; i < NUM_CLIP_PLANES; i++) {
  240.         glPushMatrix();
  241.          glRotatef(transformations[CLIPPING_TRANSFORMATION + i].x_rot, 
  242.                    1., 0., 0.);
  243.          glRotatef(transformations[CLIPPING_TRANSFORMATION + i].y_rot, 
  244.                    0., 1., 0.);
  245.          glRotatef(transformations[CLIPPING_TRANSFORMATION + i].z_rot, 
  246.                    0., 0., 1.);
  247.          glBegin(GL_LINE_LOOP);
  248.           glVertex3fv(clipplane_vertices[0]);
  249.           glVertex3fv(clipplane_vertices[1]);
  250.           glVertex3fv(clipplane_vertices[2]);
  251.           glVertex3fv(clipplane_vertices[3]);
  252.          glEnd();
  253.         glPopMatrix();
  254.     }
  255. }
  256.  
  257. static GLvoid
  258. doModelingTransformation (void)
  259. {
  260.     doTransformation(MODEL_TRANSFORMATION);
  261. }
  262.  
  263. static GLvoid
  264. drawObject (GLuint index)
  265. {
  266.     glPushMatrix();
  267.      glCallList(shapes[index].object);
  268.     glPopMatrix();
  269. }
  270.  
  271. static GLvoid
  272. drawCap (GLuint index)
  273. {
  274.     GLfloat cap_vertices[4][3] =
  275.     {
  276.         {-1., -1., 0.},
  277.         { 1., -1., 0.},
  278.         {-1.,  1., 0.},
  279.         { 1.,  1., 0.},
  280.     };
  281.     register unsigned int i = 0;
  282.  
  283.     glDepthFunc(GL_ALWAYS);
  284.     glStencilFunc(GL_EQUAL, 0x1, 0x1);
  285.     glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  286.     glEnable(GL_STENCIL_TEST);
  287.     
  288.     glEnable(GL_LIGHTING);
  289.     for (i = 0; i < NUM_CLIP_PLANES; i++) {
  290.         glPushMatrix();
  291.          doClippingTransformation(i);
  292.           glCallList(shapes[index].material_object);
  293.          glScalef(100., 100., 100.);
  294.          glBegin(GL_POLYGON);
  295.           glVertex3fv(cap_vertices[0]);
  296.           glVertex3fv(cap_vertices[2]);
  297.           glVertex3fv(cap_vertices[3]);
  298.           glVertex3fv(cap_vertices[1]);
  299.          glEnd();
  300.         glPopMatrix();
  301.     }
  302.     glDisable(GL_LIGHTING);
  303.  
  304.     glDepthFunc(GL_LESS);
  305.     glDisable(GL_STENCIL_TEST);
  306. }
  307.  
  308. static GLvoid
  309. drawCappedObject (GLuint index)
  310. {
  311.     register unsigned int i = 0;
  312.  
  313.     glPushMatrix();
  314.  
  315.      doModelingTransformation();
  316.  
  317.      for (i = 0; i < NUM_CLIP_PLANES; i++) {
  318.          glEnable(GL_CLIP_PLANE0 + i);
  319.      }
  320.  
  321.      if (do_cap) {
  322.          glStencilMask(0x01);
  323.          glEnable(GL_STENCIL_TEST);
  324.          glStencilFunc(GL_ALWAYS, 0, 0x1);
  325.          glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
  326.      }
  327.  
  328.      glEnable(GL_LIGHTING);
  329.      drawObject(index);
  330.      glDisable(GL_LIGHTING);
  331.  
  332.      if (do_cap) {
  333.          glStencilMask(STEN_MASK);
  334.      }
  335.  
  336.     glPopMatrix();
  337.  
  338.      for (i = 0; i < NUM_CLIP_PLANES; i++) {
  339.          glDisable(GL_CLIP_PLANE0 + i);
  340.      }
  341.  
  342.     if (do_cap) {
  343.         drawCap(index);
  344.     }
  345. }
  346.  
  347. static GLvoid
  348. drawInterferenceRegion (GLvoid)
  349. {
  350.     glEnable(GL_STENCIL_TEST);
  351.     glStencilFunc(GL_LESS, 0x2, STEN_MASK);
  352.     glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
  353.  
  354.     glDepthFunc(GL_ALWAYS);
  355.  
  356.     glMatrixMode(GL_PROJECTION);
  357.     glLoadIdentity();
  358.     glOrtho(0, 1, 0, 1, -1, 1);
  359.     glMatrixMode(GL_MODELVIEW);
  360.  
  361.     glPushMatrix();
  362.      glLoadIdentity();
  363.      glColor3f(1., 0., 0.);
  364.      glRectf(0., 0., 1., 1.);
  365.     glPopMatrix();
  366.  
  367.     setupProjection();
  368.  
  369.     glDisable(GL_STENCIL_TEST);
  370.     glDepthFunc(GL_LESS);
  371. }
  372.  
  373. static GLvoid
  374. updateAngles (GLvoid)
  375. {
  376.     transformations[MODEL_TRANSFORMATION].x_rot =
  377.      (transformations[MODEL_TRANSFORMATION].x_rot + ANGLE_DELTA) % 360;
  378.     transformations[MODEL_TRANSFORMATION].z_rot =
  379.      (transformations[MODEL_TRANSFORMATION].z_rot + ANGLE_DELTA) % 360;
  380. }
  381.  
  382. void 
  383. drawScene (void)
  384. {
  385.     GLdouble cp[4] = {0, 0, -1, 0};
  386.     GLuint i = 0;
  387.     GLint error = 0;
  388.  
  389.     while (error = glGetError()) {
  390.         fprintf(stderr, "ERROR : 0x%x\n", error);
  391.     }
  392.  
  393.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  394.  
  395.     for (i = 0; i < NUM_CLIP_PLANES; i++) {
  396.         glPushMatrix();
  397.          doClippingTransformation(i);
  398.          glClipPlane(GL_CLIP_PLANE0 + i, cp);
  399.         glPopMatrix();
  400.     }
  401.  
  402.     glPushMatrix();
  403.      doViewingTransformation();
  404.      whiteAxes();
  405.      drawClipPlanes ();
  406.     glPopMatrix();
  407.  
  408.     for (i = 0; i < NUMBER(shapes); i++) {
  409.         drawCappedObject(i);
  410.     }
  411.     if (do_cap) {
  412.         drawInterferenceRegion();
  413.     }
  414.  
  415.     showCurrent();
  416.     updateAngles();
  417. }
  418.  
  419.